package epoch.game.core.distr.call;

import java.io.IOException;

import org.apache.commons.lang3.builder.ToStringBuilder;

import epoch.game.core.serialize.ISerializable;
import epoch.game.core.serialize.IStreamReader;
import epoch.game.core.serialize.IStreamWriter;

/**
 * 远程调用返回 
 * @author will
 * @date 2016年4月1日 下午4:25:00
 */
public class CallReturn implements ISerializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//回调函数id
	public long id;
	//发送方NodeId	
	public int nodeId;					
	//回调处理线程
	public int threadKey;
	public boolean isActor;
	//返回值
	private Object returns;
	//错误信息
	public String err;
	
	public CallReturn() { }

	public CallReturn(long id, int nodeId, int threadKey, boolean isActor) {
		this.id = id;
		this.nodeId = nodeId;
		this.threadKey = threadKey;
		this.isActor = isActor;
	}

	@SuppressWarnings("unchecked")
	public <T> T getReturns() {
		if(returns==null){
			return null;
		}
		return (T)returns;
	}

	public void setReturns(Object returns) {
		this.returns = returns;
	}

	@Override
	public void writeTo(IStreamWriter stream) throws IOException {
		stream.writeLong(id);
		stream.writeInt(nodeId);
		stream.writeInt(threadKey);
		stream.writeBoolean(isActor);
		stream.write(returns);
		stream.writeString(err);
	}
	
	@Override
	public void readFrom(IStreamReader stream) throws IOException {
		id = stream.readLong();
		nodeId = stream.readInt();
		threadKey = stream.readInt();
		isActor = stream.readBoolean();
		returns = stream.read();
		err = stream.readString();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this)
					.append("id", id)
					.append("nodeId", nodeId)
					.append("portId", threadKey)
					.append("isActor", isActor)
					.append("err",err)
					.toString();
	}
}
